package com.haole.ecommercepay.bill.service.impl;

import cn.hutool.http.HttpResponse;
import com.alibaba.fastjson.JSONObject;
import com.haole.ecommercepay.bill.entity.FundFlowBillParam;
import com.haole.ecommercepay.bill.entity.TradeBillParam;
import com.haole.ecommercepay.bill.service.BillService;
import com.haole.ecommercepay.bill.url.BillUrl;
import com.haole.ecommercepay.common.HttpUtils;
import com.haole.ecommercepay.common.StringToExcel;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author haole
 */
@Service("billService")
public class BillServiceImpl implements BillService {

    private static final Logger logger = LoggerFactory.getLogger(BillServiceImpl.class);

    @Autowired
    private StringToExcel stringToExcel;

    /**
     * 申请交易账单
     * @param param
     * @return
     */
    @Override
    public JSONObject tradeBill(TradeBillParam param) {
        logger.info("tradeBill param:{}",param);
        String requestUrl = String.format(BillUrl.TRADE_BILL,param.getBill_date());
        if (StringUtils.isNotEmpty(param.getSub_mchid())) {
            requestUrl +=  "&sub_mchid=" + param.getSub_mchid();
        }
        if (StringUtils.isNotEmpty(param.getBill_type())) {
            requestUrl +=  "&bill_type=" + param.getBill_type();
        }
//        if (StringUtils.isNotEmpty(param.getTar_type())) {
//            requestUrl +=  "&tar_type=" + param.getTar_type();
//        }
        HttpResponse response = HttpUtils.v3HttpExecute("GET", requestUrl,
                param.getAppName(),param.getMchId(),param.getMchSerialNo(),
                param.getWechatSerialNo(),"",param.getMchPrivateKeyPath(),null);
        logger.info("tradeBill result:{}",response);
        return HttpUtils.httpResult(response,param.getWechatPubKeyPath());
    }
    /**
     * 申请资金账单
     * @param param
     * @return
     */
    @Override
    public JSONObject fundFlowBill(FundFlowBillParam param) {
        logger.info("fundFlowBill param:{}",param);
        String requestUrl = String.format(BillUrl.FUND_FLOW_BILL,param.getBill_date());
        if (StringUtils.isNotEmpty(param.getAccount_type())) {
            requestUrl +=  "&account_type=" + param.getAccount_type();
        }
//        if (StringUtils.isNotEmpty(param.getTar_type())) {
//            requestUrl +=  "&tar_type=" + param.getTar_type();
//        }
        HttpResponse response = HttpUtils.v3HttpExecute("GET", requestUrl,
                param.getAppName(),param.getMchId(),param.getMchSerialNo(),
                param.getWechatSerialNo(),"",param.getMchPrivateKeyPath(),null);
        logger.info("fundFlowBill result:{}",response);
        return HttpUtils.httpResult(response,param.getWechatPubKeyPath());
    }
    /**
     * 交易账单导出excel
     * @param type
     * @param param
     * @param response
     */
    @Override
    public void tradeBillExcel(String type, TradeBillParam param, HttpServletResponse response) {
       JSONObject tradeResult = this.tradeBill(param);
       if (tradeResult.getInteger("status") == 200) {
           String downloadUrl = tradeResult.getString("download_url");
           String suffix = downloadUrl.substring(downloadUrl.indexOf("/",downloadUrl.indexOf("/")+2));
           String hashValue =  tradeResult.getString("hash_value");
           HttpResponse res = HttpUtils.v3HttpExecute("GET", suffix,
                   param.getAppName(),param.getMchId(),param.getMchSerialNo(),
                   param.getWechatSerialNo(),"",param.getMchPrivateKeyPath(),null);
           String downloadData = res.body();
           try {
               stringToExcel.downloadBillData(downloadData,type,param.getBill_date(),response);
           } catch (IOException e) {
               e.printStackTrace();
           }
       }
    }
    /**
     * 资金账单导出excel
     * @param param
     * @param response
     */
    @Override
    public void fundFlowBillExcel(FundFlowBillParam param, HttpServletResponse response) {
        JSONObject tradeResult = this.fundFlowBill(param);
        if (tradeResult.getInteger("status") == 200) {
            String downloadUrl = tradeResult.getString("download_url");
            String suffix = downloadUrl.substring(downloadUrl.indexOf("/",downloadUrl.indexOf("/")+2));
            String hashValue =  tradeResult.getString("hash_value");
            HttpResponse res = HttpUtils.v3HttpExecute("GET", suffix,
                    param.getAppName(),param.getMchId(),param.getMchSerialNo(),
                    param.getWechatSerialNo(),"",param.getMchPrivateKeyPath(),null);
            String downloadData = res.body();
            try {
                stringToExcel.downloadBillData(downloadData,param.getBill_date(),response);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
